package com.linxz.wandnroid.appui;

import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.linxz.framework.base.BaseMVPFragment;
import com.linxz.sdkmanager.wechat.WxSdkManager;
import com.linxz.ui.GuiHelper;
import com.linxz.ui.dialogs.DialogHelper;
import com.linxz.ui.listener.QRSaveListener;
import com.linxz.ui.pojo.BannerBean;
import com.linxz.ui.widgets.BannerImageLoad;
import com.linxz.ui.widgets.LinxzRefreshLayout;
import com.linxz.wandnroid.R;
import com.linxz.wandnroid.adapter.ArticleAdapter;
import com.linxz.wandnroid.appui.view.ArticleView;
import com.linxz.wandnroid.pojo.WanAndroidArticleBean;
import com.linxz.wandnroid.presenter.ArticlePresenter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.youth.banner.Banner;
import java.util.List;
/**
 * @author Linxz
 * 创建日期：2020年02月20日 23:27
 * version：v4.5.4
 * 描述：
 */
public class ArticleFragment extends BaseMVPFragment<ArticlePresenter> implements ArticleView {

    private LinxzRefreshLayout layoutRefresh;
    private ArticleAdapter adapter;

    public static ArticleFragment newInstance() {
        return new ArticleFragment();
    }

    @Override
    protected ArticlePresenter createPresenter() {
        return new ArticlePresenter(this, lifecycleSubject);
    }

    @Override
    public Object setLayout() {
        return R.layout.wandroid_frag_article;
    }

    @Override
    public void initUI() {
        layoutRefresh = findViewById(R.id.layoutRefresh);
        RecyclerView rvArticle = findViewById(R.id.rvArticle);
        adapter = new ArticleAdapter(mvpPresenter.articleBeanList);
        GuiHelper.initRecycleView(activity, rvArticle, adapter, 1);
        adapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);

    }

    @Override
    public void initData() {
        mvpPresenter.refreshData();
    }

    @Override
    public void setListener() {
        super.setListener();
        layoutRefresh.setOnRefreshListener(new LinxzRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mvpPresenter.refreshData();
            }
        });
        layoutRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mvpPresenter.loadMoreData();
            }
        });
        adapter.setOnItemClickListener(new ArticleAdapter.OnItemClickListener() {
            @Override
            public void onClick(WanAndroidArticleBean item) {
                WanAndroidWebViewActivity.launch(activity,item.getLink());
            }

            @Override
            public void onLongClick(final WanAndroidArticleBean item) {
                DialogHelper.builder()
                        .build()
                        .showJokeShare(activity, new QRSaveListener() {
                            @Override
                            public void onShareClick() {
                                WxSdkManager.getInstance().shareArticleLink(item.getTitle(),item.getLink());
                            }

                            @Override
                            public void onSaveClick() {
                                ClipboardManager cm = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
                                ClipData mClipData = ClipData.newPlainText("Label", item.getLink());
                                if (cm != null) {
                                    cm.setPrimaryClip(mClipData);
                                    showToast("链接已复制");
                                }
                            }
                        });

            }
        });
    }

    @Override
    public void onLoadSuccess() {
        layoutRefresh.finishRefresh();
        layoutRefresh.finishLoadMore();
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onLoadError(String message, boolean showErrorLayout) {
        layoutRefresh.finishRefresh();
        layoutRefresh.finishLoadMore();
        if (showErrorLayout) {
            @SuppressLint("InflateParams") View errorView = LayoutInflater.from(activity).inflate(R.layout.ui_layout_error, null);
            adapter.setEmptyView(errorView);
        } else {
            showToast(message);
        }
    }

    @Override
    public void onLoadEmpty(boolean showEmptyLayout) {
        layoutRefresh.finishRefresh();
        layoutRefresh.finishLoadMore();
        if (showEmptyLayout){
            @SuppressLint("InflateParams") View emptyView = LayoutInflater.from(activity).inflate(R.layout.ui_layout_empty, null);
            adapter.setEmptyView(emptyView);
        }else{
            showToast("没有更多数据了");
            layoutRefresh.setNoMoreData(true);
        }
    }

    @Override
    public void onLoadBannerSuccess(List<BannerBean> bannerBeans) {
        if (!adapter.isHasHeaderView()){
            @SuppressLint("InflateParams") View headerView=getLayoutInflater().inflate(R.layout.wandroid_header_sysfrag,null);
            Banner banner=headerView.findViewById(R.id.banner);
            banner.setImageLoader(new BannerImageLoad());
            banner.setImages(bannerBeans);
            banner.start();
            adapter.addHeaderView(headerView);
            adapter.setHasHeaderView(true);
        }
    }


}
